package misc;

import core.Var;

public class Rounding {
    /*Copied from stack overflow*/  //TODO: Write our own algorithm?
    public static double round(double num, int sigFigs) {
        if (num == 0) {
            return 0;
        }

        final double d = Math.ceil(Math.log10(num < 0 ? -num : num));
        final int power = sigFigs - (int) d;

        final double magnitude = Math.pow(10, power);
        final long shifted = Math.round(num * magnitude);
        return shifted / magnitude;
    }

    public static double round(Var x) {
        return round(x.value, x.sigFig);
    }
}
